package cn.sunshine.ymcc.web.controller;

import cn.sunshine.ymcc.dto.KillParamDto;
import cn.sunshine.ymcc.service.IKillCourseService;
import cn.sunshine.ymcc.domain.KillCourse;
import cn.sunshine.ymcc.query.KillCourseQuery; // 自我修改的模板位置
import cn.sunshine.ymcc.result.JSONResult; // 自我修改的模板位置
import cn.sunshine.ymcc.result.PageList; // 自我修改的模板位置
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/killCourse")
public class KillCourseController {

    @Autowired
    public IKillCourseService killCourseService;
    /*
    * @Title: kill
    * @Description: 秒杀
    * @Author: Mr.Jiang
    * @Version: 1.0
    * @Date:  2022/8/25 22:33
    * @Parameters: [dto]
    * @Return cn.sunshine.ymcc.result.JSONResult
    */
    @RequestMapping(value="/kill",method= RequestMethod.POST)
    public JSONResult kill(@RequestBody KillParamDto killParamDto){
        // 执行秒杀然后返回预创订单号
        String orderNo = killCourseService.kill(killParamDto);
        return JSONResult.success(orderNo);
    }


    // /online/one/"+this.killId + "/" +this.activityId
    /*
    * @Title: onlineAllKillCourse
    * @Description: 查询秒杀的指定课程
    * @Author: Mr.Jiang
    * @Version: 1.0
    * @Date:  2022/8/25 19:50
    * @Parameters: []
    * @Return cn.sunshine.ymcc.result.JSONResult
    */
    @RequestMapping(value = "/online/one/{killId}/{activityId}",method = RequestMethod.GET)
    public JSONResult onlineone(@PathVariable("killId") Long killId,@PathVariable("activityId") Long activityId){
        KillCourse onlineKillCourse = killCourseService.onlineone(killId,activityId);
        return JSONResult.success(onlineKillCourse);
    }
    /**
     * 查询所有发布到Redis上的秒杀课程
     */
    @RequestMapping(value = "/online/all",method = RequestMethod.GET)
    public JSONResult onlineAllKillCourse(){
       List<KillCourse> allKillCourse = killCourseService.onlineAllKillCourse();
        return JSONResult.success(allKillCourse);
    }

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/add",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody KillCourse killCourse){
        if(killCourse.getId()!=null){
            killCourseService.updateById(killCourse);
        }else{
            killCourseService.add(killCourse);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        killCourseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(killCourseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(killCourseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody KillCourseQuery query){
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(),query.getRows());
        page = killCourseService.selectPage(page);
        return JSONResult.success(new PageList<KillCourse>(page.getTotal(),page.getRecords()));
    }
}
